function intoRoma(num) {
  let arr = new Array();
  let i = Math.floor(num / 1000);
  let objH = {
    num: Math.floor(num / 100) % 10,
    nName: 'h'
  }
  let objT = {
    num: Math.floor(num / 10) % 10,
    nName: 't'
  }
  let objN = {
    num: num % 10,
    nName: 'n'
  }
  for (let t = 0; t < i; t++) { 
    arr.push('M')
  }
  arr.push(...commonConvert(objH));
  arr.push(...commonConvert(objT));
  arr.push(...commonConvert(objN));
  return arr.join('');
}
function commonConvert(numObj) {
  let num1 = '', num2 = '', num3 = '';
  let n = numObj.num;
  let temp = new Array();
  switch (numObj.nName) { 
    case 'h':
      num1 = 'C';
      num2 = 'D';
      num3 = 'M';
      break;
    case 't':
      num1 = 'X';
      num2 = 'L';
      num3 = 'C';
      break;
    case 'n':
      num1 = 'I';
      num2 = 'V';
      num3 = 'X';
      break;
  }
  if (n <= 0) {
    return '';
  } else { 
    if (n <= 3) {
      for (let nn = 0; nn < n; nn++) {
        temp.push(num1);
      }
    } else if (n == 4) {
      temp.push(num1 + num2);
    } else if (n == 5) {
      temp.push(num2);
    } else if (n <= 8) {
      temp.push(num2);
      for (let nn = 0; nn < n - 5; nn++) {
        temp.push(num1);
      }
    } else { 
      temp.push(num1 + num3);
    }
  }
  return temp;
}
console.log(intoRoma(3));
console.log(intoRoma(4));
console.log(intoRoma(9));
console.log(intoRoma(58));
console.log(intoRoma(1994));